* Replication
* John Gerring, Carl Henrik Knutsen, Matthew Maguire, Svend-Erik Skaaning, Jan Teorell, and Michael Coppedge. 2021. “Democracy and Human Development: Issues of Conceptualization and Measurement.” Democratization 28(2): 308-332.

* Table 2: MPI and mortality

* Model 1

* set maxvar
clear
clear matrix
clear mata
set maxvar 20000

* set directory

* load data
use "2021_Democratization_replication_data_posteriors.dta", clear
drop country_name country_text_id country v2x_polyarchy-durable_ln
order country_id year

set matsize 10000

* Collapse by decade using 10-year averages
xtset country_id year
gen decade = 10 * floor(year/10)
foreach var of varlist mpi_v901_stock_10-mpi_med_stock_10 {
qui egen mean_`var' = mean(`var'), by(country_id decade)
qui replace `var' = mean_`var'
qui drop mean_`var'
}
*end
drop year
duplicates drop
gen trend = .
replace trend = 1 if decade == 1900
replace trend = 2 if decade == 1910
replace trend = 3 if decade == 1920
replace trend = 4 if decade == 1930
replace trend = 5 if decade == 1940
replace trend = 6 if decade == 1950
replace trend = 7 if decade == 1960
replace trend = 8 if decade == 1970
replace trend = 9 if decade == 1980
replace trend = 10 if decade == 1990
replace trend = 11 if decade == 2000
replace trend = 12 if decade == 2010
rename decade year
* order
order country_id year trend

* add other variables (everything not from V-Dem)
merge 1:1 country_id year using "2021_Democratization_replication_data_10yr.dta", keepusing(imr_gapminder_ipo_ln e_migdppcln_ipo e_miurbani e_miferrat_ln e_migdpgro e_miinterc e_miinteco v2x_corr_stock_10 wdi_mort_ln cme_gapminder_ipo_ln le_gapminder_ipo_85mln)
keep if _merge == 3
drop _merge

* put democracy index on 0-1 scale
foreach x of varlist mpi_med_stock_10 {
quietly sum `x'
quietly gen `x'_scale = (((`x' - r(min)) * (1 - 0)) / (r(max) - r(min)))
quietly replace `x' = `x'_scale
quietly drop `x'_scale
}
*

* sort
qui sort country_id year
qui xtset country_id trend

* Run baseline model
qui xtreg F.imr_gapminder_ipo_ln mpi_med_stock_10 e_migdppcln_ipo i.year, fe vce(cluster country_id)
qui outreg2 using Table_2_Model_1.dta, label se bdec(3) noni noobs nor nocon nonot drop(i.year) ctitle(" ") addstat("Years", e(g_max), "Countries", e(N_g), "Observations", e(N), "R-squared", e(r2_w)) dta replace

***Monte Carlo Estimates Using UDS 3202 Draw Posterior Distribution***
*Run the monte carlo

forvalues i = 2(1)901 {

*Print out an iteration number
display `i'

* put democracy index on 0-1 scale
foreach x of varlist mpi_v`i'_stock_10 {
quietly sum `x'
quietly gen `x'_scale = (((`x' - r(min)) * (1 - 0)) / (r(max) - r(min)))
quietly replace `x' = `x'_scale
quietly drop `x'_scale
}
*

*Fit the model, using the ith draw from the UDS posterior
quietly xtreg F.imr_gapminder_ipo_ln mpi_v`i'_stock_10 e_migdppcln_ipo i.year, fe vce(cluster country_id)
qui outreg2 using Table_2_Model_1.dta, label se bdec(3) noni noobs nor nocon nonot drop(i.year) ctitle("`i'") addstat("Years", e(g_max), "Countries", e(N_g), "Observations", e(N), "R-squared", e(r2_w)) dta append

*Extract the coefficients and variance-covariance matrix
matrix b = e(b)
matrix V = e(V)
local blength = colsof(b)

*Preserve the dataset, take a single multivariate normal draw from the
*posterior distribution of the coefficients, and restore the dataset.
*We use the capture command to catch possible errors in drawnorm
*and drop these iterations gracefully.
preserve 
capture quietly drawnorm b1-b`blength', double n(1) means(b) cov(V) clear
if _rc==0 {
mkmat b1-b`blength', matrix(bsample)
matrix posterior = nullmat(posterior) \ bsample
}
else {
display "Error drawing sample...iteration dropped"
}
restore

*Closes the Monte Carlo Loop
}

*Get posterior ready to work with
svmat posterior

*Calculate means and standard deviations
*tabstat posterior*, stat(mean sd)
keep posterior*
quietly outreg2 using Table_2_Model_1, eqkeep(mean sd) bdec(3) replace sum(log) excel
